package com.jixiantubu.denglu;

import org.apache.http.Header;

import com.example.jixiantubu.R;
import com.jixiantubu.jinnang.JinNang_YouJi_Find;
import com.jixiantubu.util.CustomProgressDialog;
import com.jixiantubu.util.MyApplication;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

public class Password extends Activity {
	EditText firstpasswordEditText, secondpasswordEditText;
	MyApplication myApplication;
	String limit = "1234567890abcd...xyzASDC...XYZ";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.password);
		firstpasswordEditText = (EditText) findViewById(R.id.EditText01);
		firstpasswordEditText.setKeyListener(DigitsKeyListener
				.getInstance(limit));
		secondpasswordEditText = (EditText) findViewById(R.id.EditText02);
		secondpasswordEditText.setKeyListener(DigitsKeyListener
				.getInstance(limit));
		myApplication = new MyApplication();
	}

	public void back(View view) {
		finish();

	}

	@SuppressLint("NewApi")
	public void jump(View view) {
		final CustomProgressDialog pd = new CustomProgressDialog(Password.this,
				"正在拼命加载数据,请稍等！", R.anim.frame);
		pd.show();
		String username = IphoneNumber.phone();
		String firstpasswordString = firstpasswordEditText.getText().toString()
				.trim();
		String secondpasswordString = secondpasswordEditText.getText()
				.toString().trim();
		MyApplication.httpclient.configSoTimeout(5000);
		if (firstpasswordString.equals(secondpasswordString)) {
			AsyncHttpClient client = new AsyncHttpClient();
			String url = "http://" + MyApplication.getIP()
					+ ":8080/Jinnang/Register?user_name=" + username
					+ "&user_password=" + firstpasswordString;
			client.get(url, new AsyncHttpResponseHandler() {

				@Override
				public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
					pd.dismiss();
					String text = new String(arg2);
					if (text.equals("success") || text.equals("chenggong")) {
						runOnUiThread(new Runnable() {
							public void run() {
								Intent intent = new Intent(Password.this,
										Login.class);
								startActivity(intent);
							}
						});
					} else {
						runOnUiThread(new Runnable() {
							public void run() {
								Toast.makeText(
										Password.this,
										"Sorry,your name or password is wrong!",
										0).show();

							}
						});
					}

				}

				@Override
				public void onFailure(int arg0, Header[] arg1, byte[] arg2,
						Throwable arg3) {
					pd.dismiss();
					MyApplication.show(Password.this, "加载数据失败，请检查网络!");

				}
			});
		} else {
			Toast.makeText(Password.this,
					"Sorry,your Two times the password is not consistent!", 0)
					.show();
		}

	}

}
